--
-- DESCRIPTION
--
-- @COMPANY **
-- @AUTHOR **
-- @DATE ${date} ${time}
--

---@type BP_Samurai_C
local M = UnLua.Class()

local GameplayStatics = UE.UGameplayStatics
local GM_Duel_Class = UE.UClass.Load("/Script/Engine.Blueprint'/QuickDraw/Blueprints/GM_Duel.GM_Duel_C'")

local KismetMathLibrary = UE.UKismetMathLibrary

--function M:Initialize(Initializer)
--end

--function M:UserConstructionScript()
--end

-- 初始化
function M:Initialize()

    -- 获取游戏模式
    self.GameMode = GameplayStatics.GetGameMode(self:GetWorld()):Cast(GM_Duel_Class)

    -- 设置默认的坐标
    self.SpriteDefaultPos = self.PaperSprite.RelativeLocation
    -- 设置默认的动画
    self:SetSourceSprite(self.IdleSprite)

    self.SlideInAnim:AddInterpFloat(self.SlideInCurve, { self, self.SlideInUpdateFunc }) -- 滑入时间轴绑定曲线并且绑定更新函数
    self.SlideInAnim:SetTimelineFinishedFunc({ self, self.SlideInFinishedFunc }) -- 滑入时间轴绑定完成函数

    self.AttackAnim:AddInterpFloat(self.AttackCurve, { self, self.AttackAnimUpdateFunc }) -- 攻击时间轴绑定曲线并且绑定更新函数
    self.AttackAnim:SetTimelineFinishedFunc({ self, self.AttackAnimFinishedFunc }) -- 攻击时间轴绑定完成函数
    self.AttackAnim:AddEvent(0.6, { self, self.AttackAnimEventFunc }) -- 攻击时间轴自定义时间事件绑定函数
end

function M:ReceiveBeginPlay()
    self:Initialize()
    self:SlideIn()
    self:SetCrossVisibility(false)
end

--function M:ReceiveEndPlay()
--end

-- function M:ReceiveTick(DeltaSeconds)
-- end

-- function M:ReceiveAnyDamage(Damage, DamageType, InstigatedBy, DamageCauser)
-- end

-- function M:ReceiveActorBeginOverlap(OtherActor)
-- end

-- function M:ReceiveActorEndOverlap(OtherActor)
-- end

--- 设置源图片
--- @param newSourceSprite UPaperSprite 输入的源图片
function M:SetSourceSprite(newSourceSprite)
    if newSourceSprite then
        self.PaperSprite:SetSprite(newSourceSprite)
    end
end

--- 无效攻击显示
function M:SetCrossVisibility(Visibility)
    self.Cross:SetVisibility(Visibility)
end

-- 滑入
function M:SlideIn()
    self.SlideInAnim:SetPlayRate(1 / self.SlideTime)
    self.SlideInAnim:PlayFromStart()
end

function M:SlideInUpdateFunc(value)
    local EndLocation = KismetMathLibrary.VLerp(self.SlideStartPos, self.SpriteDefaultPos, value)
    self.PaperSprite:K2_SetRelativeLocation(EndLocation, false, nil, false)

end

-- 滑入动画时间轴完成
function M:SlideInFinishedFunc()

end

-- 攻击位置更新
function M:AttackAnimUpdateFunc(value)
    local EndLocation = KismetMathLibrary.VLerp(self.SpriteDefaultPos, self.AttackPos.RelativeLocation, value)
    self.PaperSprite:K2_SetRelativeLocation(EndLocation, false, nil, false)
end

-- 攻击停止
function M:AttackAnimFinishedFunc()
    self.GameMode:AttackSuccessful(self:IsPlayerControlled())
end

function M:AttackAnimEventFunc()
    self:SetSourceSprite(self.AttackSprite)
end

function M:Attack()
    if self.PressedDraw then
        return
    end

    if self.GameMode.Phase == UE.EPhase.EP_Intro then
    elseif self.GameMode.Phase == UE.EPhase.EP_Wait then
        self:SetCrossVisibility(true)
        self.PressedDraw = true
    elseif self.GameMode.Phase == UE.EPhase.EP_Draw then
        self.AttackAnim:SetPlayRate(1 / 0.125)
        self.AttackAnim:PlayFromStart()
        self.GameMode.ExclamationRef:SetVisibility(false)
        self.PressedDraw = true
        self.GameMode:SetGameFinished()
    elseif self.GameMode.Phase == UE.EPhase.EP_Finished then

    end
end

--- 击败
function M:Defeated()
    self:SetSourceSprite(self.DefeatedSprite)
end

--- 重置
function M:Reset()
    self:SetSourceSprite(self.IdleSprite)
    self.PressedDraw = false
    self:SlideIn()
    self:SetCrossVisibility(false)
end

return M
